package com.person.jiaiplan.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.person.jiaiplan.assembler.CalendarAssembler;
import com.person.jiaiplan.pojo.dto.calendar.CalendarQueryResDTO;
import com.person.jiaiplan.pojo.dto.calendar.CommemorationDayResDTO;
import com.person.jiaiplan.pojo.entity.CommemorationDayConfig;
import com.person.jiaiplan.pojo.signle.SignleSnowFlake;
import com.person.jiaiplan.service.CommemorationDayConfigService;
import com.person.jiaiplan.service.JiAiCalendarService;
import com.person.jiaiplan.service.ScheduleCommonQueryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class jiAiCalendarServiceImpl implements JiAiCalendarService {

    @Autowired
    private ScheduleCommonQueryService queryService;

    @Autowired
    private CommemorationDayConfigService commemorationDayConfigService;


    @Override
    public CalendarQueryResDTO queryCalendarByDay(String dateTime) {
        return queryService.queryCalendarByDay(dateTime);
    }

    @Override
    public Boolean addCommemorationDay(CommemorationDayResDTO resDTO) {
        CommemorationDayConfig config = new CommemorationDayConfig();
        config.setId(SignleSnowFlake.getSnowflakeId());
        config.setCommemorationType(resDTO.getCommemorationType());
        config.setRemark(resDTO.getRemark());
        config.setTime(DateUtil.parseDate(resDTO.getTime()));
        config.setTimeType(resDTO.getTimeType());
        config.setCreateTime(DateUtil.date());
        config.setContent(resDTO.getContent());
        return commemorationDayConfigService.save(config);
    }

    @Override
    public List<CommemorationDayResDTO> getCommemorationDay(String dateTime, String type) {
        if (StrUtil.isBlank(dateTime) || "today".equals(type)) {
            dateTime = DateUtil.today();
        } else {
            dateTime = dateTime.substring(0, 4) + "-" + StrUtil.replace(dateTime.substring(6, 8), " ", "0") + "-01";
            if ("prev-month".equals(type)) {
                dateTime = DateUtil.formatDate(DateUtil.offsetMonth(DateUtil.parseDate(dateTime), -1));
            }
            if ("next-month".equals(type)) {
                dateTime = DateUtil.formatDate(DateUtil.offsetMonth(DateUtil.parseDate(dateTime), 1));
            }
        }

        String startTime = DateUtil.beginOfMonth(DateUtil.parseDate(dateTime)).toString();
        String endTime = DateUtil.endOfMonth(DateUtil.parseDate(dateTime)).toString();

        List<CommemorationDayConfig> commemorationDayConfigList = commemorationDayConfigService.list(Wrappers.<CommemorationDayConfig>lambdaQuery());


        List<CommemorationDayResDTO> commemorationDayResDTOS = new ArrayList<>();
        String finalDateTime = dateTime;
        commemorationDayConfigList.forEach(item -> {
            // 规则：如果是1 一年一次 则将年替换成要查询的年
            if (item.getCommemorationType() == 1) {
                item.setTime(DateUtil.parseDate(finalDateTime.substring(0, 4) + "-" + (DateUtil.month(item.getTime()) + 1) + "-" + DateUtil.dayOfMonth(item.getTime())));
            }
            // 规则：如果是2 一月一次，则将年和月替换成查询的
            else if (item.getCommemorationType() == 2) {
                // 日期前部零补齐
                String date = DateUtil.dayOfMonth(item.getTime()) > 10 ? String.valueOf(DateUtil.dayOfMonth(item.getTime())) : "0" + DateUtil.dayOfMonth(item.getTime());
                item.setTime(DateUtil.parseDate(finalDateTime.substring(0, 4) + "-" + finalDateTime.substring(5, 7) + "-" + date));
            }
            CommemorationDayResDTO commemorationDayResDTO = CalendarAssembler.INSTANCE.toCommemorationDayResDTO(item);
            commemorationDayResDTOS.add(commemorationDayResDTO);
        });

        // 筛选 日期在时间范围内的
        List<CommemorationDayResDTO> result = commemorationDayResDTOS.stream().filter(item -> {
            return DateUtil.parseDate(item.getTime()).isAfter(DateUtil.parse(startTime)) && DateUtil.parseDate(item.getTime()).isBefore(DateUtil.parse(endTime));
        }).collect(Collectors.toList());


        return result;
    }
}
